package cn.com.chengmandian.bike.rabbitmq;

import cn.com.chengmandian.bike.model.EbMemberPay;
import cn.com.chengmandian.bike.model.EbRefundInfo;
import cn.com.chengmandian.bike.service.*;
import cn.com.chengmandian.core.redis.cache.AgentCacheService;
import cn.com.chengmandian.core.web.pojo.AgentInfo;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.com.chengmandian.rabbitmq.constants.RabbitMqConstants;
import cn.com.chengmandian.rabbitmq.pojo.BikeRefundResult;
import cn.com.chengmandian.service.bike.constants.RefundBusinessTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Slf4j
@Component
public class BikeRefundReceiver {

    @Autowired
    private IEbRefundInfoService refundInfoService;

    @Autowired
    private IEbMemberWalletService memberWalletService;

    @Autowired
    private AgentCacheService agentCacheService;

    @Autowired
    private IEbMemberPayService memberPayService;

    @Autowired
    private IEbMemberCardService memberCardService;

    @Autowired
    private IEbMemberComboRecordService memberComboRecordService;

    @Autowired
    private IEbRentOrderService rentOrderService;

    @RabbitListener(queues = RabbitMqConstants.QUEUE_BIKE_REFUND, ackMode = "AUTO")
    public void refund(BikeRefundResult refundResult) {
        try {
            if (refundResult == null) {
                log.error("refundResult数据为空");
                return;
            }
            log.info("退款结果数据：{}", refundResult);

            AgentInfo agentInfo = agentCacheService.getAgent(refundResult.getAgentNo());
            LocalMapUtil.setAgentInfo(agentInfo);

            EbRefundInfo ebRefundInfo = refundInfoService.getOne(new LambdaQueryWrapper<EbRefundInfo>()
                    .eq(EbRefundInfo::getRefundNo, refundResult.getRefundNo())
                    .last("limit 0,1"));
            Assert.notNull(ebRefundInfo,"退款记录不存在!");
            ebRefundInfo.setChannelNo(refundResult.getChannelNo());
            EbMemberPay memberPay = memberPayService.getById(ebRefundInfo.getPayId());

            if (ebRefundInfo.getBusinessType().equals(RefundBusinessTypeEnum.YAJIN.getCode())) {
                refundInfoService.refundSuccess(refundResult.getRefundNo(), refundResult.getChannelNo());
            } else if (ebRefundInfo.getBusinessType().equals(RefundBusinessTypeEnum.MONEY.getCode())) {
                memberWalletService.walletRefundSuccess(ebRefundInfo, memberPay.getPayNo());
            } else if (ebRefundInfo.getBusinessType().equals(RefundBusinessTypeEnum.ORDER.getCode())) {
                refundInfoService.refundSuccess(refundResult.getRefundNo(), refundResult.getChannelNo());
            } else if (ebRefundInfo.getBusinessType().equals(RefundBusinessTypeEnum.CARD.getCode())) {
                memberCardService.rideCardRefundSuccess(ebRefundInfo, memberPay.getPayNo());
            } else if (ebRefundInfo.getBusinessType().equals(RefundBusinessTypeEnum.RENT.getCode())) {
                rentOrderService.rentRefundSuccess(ebRefundInfo, memberPay.getPayNo());
            } else if (ebRefundInfo.getBusinessType().equals(RefundBusinessTypeEnum.RENT_PENALTY.getCode())) {
                rentOrderService.rentRefundSuccess(ebRefundInfo, memberPay.getPayNo());
            } else if (ebRefundInfo.getBusinessType().equals(RefundBusinessTypeEnum.COMBO.getCode())) {
                memberComboRecordService.refundSuccess(ebRefundInfo, memberPay.getPayNo());
            } else if (ebRefundInfo.getBusinessType().equals(RefundBusinessTypeEnum.RIDE_PENALTY.getCode())) {
                refundInfoService.refundSuccess(refundResult.getRefundNo(), refundResult.getChannelNo());
            }

        } catch (Exception e) {
            log.error("处理退款结果错误", e);
        }
    }
}
